package com.dabai.applicationmanager.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dabai.applicationmanager.R;
import com.dabai.applicationmanager.adapter.SmallProgramAdapter;
import com.dabai.applicationmanager.bean.SmallProgramInfo;
import com.dabai.applicationmanager.databinding.ActivitySmallProgramBinding;
import com.dabai.applicationmanager.ui.smallprogram.AddShellShortcutActivity;
import com.dabai.applicationmanager.ui.smallprogram.BatchRenameActivity;
import com.dabai.applicationmanager.ui.smallprogram.FileTransferActivity;
import com.dabai.applicationmanager.ui.smallprogram.FreezerActivity;
import com.dabai.applicationmanager.ui.smallprogram.ScanInstallationPackageActivity;
import com.dabai.applicationmanager.utils.OtherUtils;
import com.dabai.dbutils.dialog.MdcDialog;
import com.dabai.dbutils.qrcode.QRCodeUtils;
import com.dabai.dbutils.toast.DBToast;
import com.dabai.dbutils.utils.AppUtils;
import com.dabai.dbutils.utils.DabaiUtils;
import com.dabai.dbutils.utils.component.ActivityUtils;
import com.dabai.uitools.base.BaseAppCompatActivity;

import java.util.ArrayList;
import java.util.Objects;

public class SmallProgramActivity extends BaseAppCompatActivity {

    Context context;
    ActivitySmallProgramBinding aspd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        aspd = ActivitySmallProgramBinding.inflate(getLayoutInflater());
        setContentView(aspd.getRoot());

        context = this;

        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        aspd.recyclerView.setLayoutManager(new GridLayoutManager(this, 2));

        initSmallProgramList();

    }

    public void initSmallProgramList() {

        //准备数据
        ArrayList<SmallProgramInfo> smallProgramInflows = new ArrayList<>();

        smallProgramInflows.add(new SmallProgramInfo("扫描软件包"
                , "管理外部储存的安装包"
                , R.drawable.ic_round_android_24
                , ScanInstallationPackageActivity.class));

        smallProgramInflows.add(new SmallProgramInfo("冷藏室"
                , "被停用的应用都在这里"
                , R.drawable.ic_outline_not_interested_24
                , FreezerActivity.class));

        smallProgramInflows.add(new SmallProgramInfo("扫描应用码"
                , "扫描应用商店跳转链接"
                , R.drawable.ic_outline_qr_code_scanner_24
                , null));

        smallProgramInflows.add(new SmallProgramInfo("应用传输"
                , "通过局域网技术,实现内网互传"
                , R.drawable.ic_outline_wifi_tethering_24
                , FileTransferActivity.class));

        smallProgramInflows.add(new SmallProgramInfo("批量重命名"
                , "把文件夹内的安装包按照规则重命名"
                , R.drawable.ic_baseline_list_alt_24
                , BatchRenameActivity.class));

        smallProgramInflows.add(new SmallProgramInfo("shell快捷方式"
                , "把shell命令发送到桌面"
                , R.drawable.ic_baseline_code_24
                , AddShellShortcutActivity.class));


        //准备适配器
        SmallProgramAdapter smallProgramAdapter = new SmallProgramAdapter(smallProgramInflows);
        smallProgramAdapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.AlphaIn);

        //设置适配器
        aspd.recyclerView.setAdapter(smallProgramAdapter);

        smallProgramAdapter.setOnItemClickListener((adapter, view, position) -> {

            int pos = position + 1;

            if (pos == 3) {
                QRCodeUtils.startQrCodeScanner(this, 201);
                return;
            }


            // finish();
            ActivityUtils.startActivity(context, ((SmallProgramInfo) adapter.getData().get(position)).getTagClass());
        });

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 201) {
                if (data != null) {
                    var link = QRCodeUtils.getQrCodeScannerResult(data);
                    if (link.startsWith("market://")) {
                        DabaiUtils.openLink(context, link);
                    } else {
                        DBToast.showYellowToast(context, "未扫描到应用商店链接!");
                    }
                }
            }
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}